﻿/* 
 *  <copyright file="GamepadDevice.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;

namespace SobrietyEngine.PathFinding
{
    public class Edge : SobrietyEngine.PathFinding.IEdge
    {
        public Object UserData { get; set; }
        public INode[] Nodes { get; private set; }
        public INode Start { get { return Nodes[0]; }  }
        public INode End { get { return Nodes[1]; } }
        public float Weight { get; set; }
        public Boolean OneWay { get; set; }

        public Edge()
        {
            Nodes = new INode[2];
            Weight = 1.0f;
            OneWay = false;
        }

        public Edge(INode start, INode end)
            :this()
        {
            Nodes[0] = start;
            Nodes[1] = end;
            start.Edges.Add(this);
            end.Edges.Add(this);
        }

        public Edge(INode start, INode end, Boolean oneWay)
            :this(start, end)
        {
            OneWay = oneWay;
        }

        public Boolean IsOut(INode node)
        {
            if (OneWay)
            {
                if (End == node)
                    return true;
            }
            else
            {
                if (Start == node || End == node)
                    return true;
            }
            return false;
        }

        public Boolean IsIn(INode node)
        {
            if (OneWay)
            {
                if (Start == node)
                    return true;
            }
            else
            {
                if (Start == node || End == node)
                    return true;
            }
            return false;
        }

        public INode GetOtherNode(INode node)
        {
            if (node == Start)
                return End;
            else if (node == End)
                return Start;
            throw new Exception("Node not part of edge");
        }
    }
}
